package admin

import (
	"beego/models"
	"beego/models/admin"
	"github.com/astaxie/beego"
	"strconv"
	"sync"
)

var wg sync.WaitGroup

type AdminController struct {
	beego.Controller
}

func (c *AdminController) Get() {
	admins := make([]admin.Admin, 0)
	models.DB.Preload("Role").Order("id desc ,id").Find(&admins)
	c.Data["adminList"] = admins
	c.Data["adminListLen"] = len(admins)
	c.TplName = "admin/admin/index.html"
}

func (c *AdminController) Add() {
	role := make([]admin.Role, 0)
	models.DB.Find(&role)
	c.Data["role"] = role
	c.TplName = "admin/admin/add.html"
}

func (c *AdminController) DoAdd() {
	account := c.GetString("account")
	password := c.GetString("password")
	repassword := c.GetString("repassword")
	nickname := c.GetString("nickname")
	roleIds := c.GetStrings("role_ids")
	if password != repassword {
		data := &JSONS{-1, "两次密码不一致", []string{}}
		c.Data["json"] = data
		c.ServeJSON()
		return
	}
	admins := admin.Admin{}
	models.DB.Where("account = ?", account).First(&admins)
	if admins.Id != 0 {
		data := &JSONS{-1, "该账号已经存在", []string{}}
		c.Data["json"] = data
		c.ServeJSON()
		return
	}
	admins.Account = account
	admins.Password = models.Md5(password)
	admins.Nickname = nickname
	admins.Ctime = int(models.GetUnix())
	err := models.DB.Create(&admins).Error
	if err != nil {
		data := &JSONS{-1, "数据插入失败", []string{}}
		c.Data["json"] = data
		c.ServeJSON()
		return
	}
	//保存管理员对应的角色数据
	if roleIds == nil {
		data := &JSONS{200, "数据插入成功", []string{}}
		c.Data["json"] = data
		c.ServeJSON()
		return
	}
	wg.Add(1) //开启一个协程
	go func() {
		for _, value := range roleIds {
			adminRole := admin.AdminRole{}
			rid, _ := strconv.Atoi(value)
			adminRole.AdminId = admins.Id
			adminRole.Ctime = int(models.GetUnix())
			adminRole.RoleId = rid
			err := models.DB.Create(&adminRole).Error
			if err != nil {
				beego.Notice("角色插入数据插入失败" + value)
			} else {
				beego.Notice("角色插入成功" + value)
			}
		}
		wg.Done() //子协成执行完成
	}()
	wg.Wait() //等待子协成执行完成
	data := &JSONS{200, "数据插入成功", []string{}}
	c.Data["json"] = data
	c.ServeJSON()
	return
}

func (c *AdminController) Edit() {
	id := c.GetString("id")
	adminList := admin.Admin{}
	models.DB.Preload("Role").Where("id = ?", id).First(&adminList)
	roleList := make([]admin.Role, 0)
	models.DB.Find(&roleList)

	//把当前用户拥有的权限的id  放到map对象里
	roleAccessMap := make(map[int]int)
	for _, v := range adminList.Role {
		roleAccessMap[v.Id] = v.Id
	}

	//循环遍历所有的权限数据，判断当前权限的id是否在角色权限的Map对象中,如果是的话给当前数据加入checked属性
	for i := 0; i < len(roleList); i++ {
		if _, ok := roleAccessMap[roleList[i].Id]; ok {
			roleList[i].Checked = true
		}
	}

	c.Data["adminList"] = adminList
	c.Data["roleList"] = roleList
	c.TplName = "admin/admin/edit.html"
	//c.Data["json"] = roleList
	//c.ServeJSON()
}

//执行修改
func (c *AdminController) DoEdit() {
	id, err := c.GetInt("id")
	if err != nil {
		data := &JSONS{-1, "传递数据不正确", []string{}}
		c.Data["json"] = data
		c.ServeJSON()
		return
	}

	account := c.GetString("account")
	password := c.GetString("password")
	repassword := c.GetString("repassword")
	nickname := c.GetString("nickname")
	roleIds := c.GetStrings("role_ids")

	admins := admin.Admin{}
	models.DB.Where("Id = ?", id).First(&admins)
	if len(repassword) != 0 && len(password) != 0 {
		admins.Password = models.Md5(password)
	}
	admins.Account = account
	admins.Nickname = nickname
	err1 := models.DB.Save(&admins).Error
	if err1 != nil {
		data := &JSONS{-1, "管理员修改失败", []string{}}
		c.Data["json"] = data
		c.ServeJSON()
		return
	}
	err2 := models.DB.Where("admin_id = ?", id).Delete(admin.AdminRole{}).Error
	if err2 != nil {
		beego.Notice("该管理员原角色数据删除失败")
	}
	wg.Add(1) //开启一个协程
	go func() {
		for _, value := range roleIds {
			adminRole := admin.AdminRole{}
			rid, _ := strconv.Atoi(value)
			adminRole.AdminId = admins.Id
			adminRole.Ctime = int(models.GetUnix())
			adminRole.RoleId = rid
			err := models.DB.Create(&adminRole).Error
			if err != nil {
				beego.Notice("角色插入数据插入失败" + value)
			} else {
				beego.Notice("角色插入成功" + value)
			}
		}
		wg.Done() //子协成执行完成
	}()
	wg.Wait() //等待子协成执行完成
	data := &JSONS{200, "管理员修改成功", []string{}}
	c.Data["json"] = data
	c.ServeJSON()
	return
}

func (c *AdminController) Delete() {
	id, err := c.GetInt("id")
	if err != nil {
		beego.Info("id参数类型传输错误")
		return
	}
	adminList := admin.Admin{Id: id}
	err2 := models.DB.Delete(&adminList).Error
	if err2 != nil {
		data := &JSONS{200, "管理员删除失败", []string{}}
		c.Data["json"] = data
		c.ServeJSON()
	}
	data := &JSONS{200, "管理员删除成功", []string{}}
	c.Data["json"] = data
	c.ServeJSON()
}
